package com.canxin.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.canxin.entity.BinduserEntity;
import com.canxin.entity.ExamEntity;
import com.canxin.entity.NoticeEntity;
import com.canxin.entity.ScoreEntity;
import com.canxin.service.BinduserServiceI;
import com.canxin.service.ExamServiceI;
import com.canxin.service.NoticeServiceI;
import com.canxin.service.ScoreServiceI;

/**   
 * @Title: Controller
 * @Description: 用于存放绑定用户的信息
 * @author zhangdaihao
 * @date 2017-03-12 15:17:37
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/noticeController")
public class NoticeController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(NoticeController.class);

	@Autowired
	private NoticeServiceI noticeService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private ExamServiceI examService;
	@Autowired
	private BinduserServiceI binduserService;
	@Autowired
	private ScoreServiceI scoreService;	
	
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 用于存放绑定用户的信息列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "sendNotice",produces="application/json;charset=UTF-8")
	@ResponseBody
	public AjaxJson sendNotice(HttpServletRequest request) throws IOException {
		AjaxJson j = new AjaxJson();
		String requestParamsStr = request.getParameter("message");
		JSONObject reqJson = (JSONObject) JSONObject.parse(requestParamsStr);
		reqJson.put("type", "");
		noticeService.sendNoticeToUser(reqJson);
		j.setSuccess(true);
		j.setMsg("success");
		return j;
		
	}

	@RequestMapping(params = "sendPreNotice")
	@ResponseBody
	public AjaxJson sendPreNotice(NoticeEntity notice,HttpServletRequest request) throws IOException {
		AjaxJson j = new AjaxJson();
		String requestParamsStr = request.getParameter("message");
		JSONObject reqJson = (JSONObject) JSONObject.parse(requestParamsStr);
		reqJson.put("type", "pre");
		reqJson.put("sendto", "");
		noticeService.sendNoticeToUser(reqJson);
		j.setSuccess(true);
		return j;
	}
	
	@RequestMapping(params = "sendStudentScoreNoticeToBindUser")
	@ResponseBody
	public AjaxJson sendStudentScoreNoticeToBindUser(HttpServletRequest request) throws IOException {
		AjaxJson j = new AjaxJson();
		ArrayList openIdList= new ArrayList();
		openIdList.add("ofPl_wkuIrE5yOa5Xnt9v3cugvbY");
		List<BinduserEntity> bindUserList = binduserService.getAllBindUser();
		ExamEntity currentExamEntity = examService.getAllExamsOrderByExamDate().get(0);
		Map params = new HashMap();
		for(int i=0;i<bindUserList.size();i++)
		{
			String openId = bindUserList.get(i).getOpenid();
/*			if(!openId.equals("ofPl_wkuIrE5yOa5Xnt9v3cugvbY"))
				continue;*/
			String studentId = bindUserList.get(i).getStudentid();
			ScoreEntity scoreEntity  = scoreService.getScoresByStudentIdAndExamId(studentId, currentExamEntity.getExamid());
			if(scoreEntity==null)
				continue;
			params.put("first", "您好，"+scoreEntity.getStudentname()+"同学的考试成绩如下");
			params.put("studentName",scoreEntity.getStudentname());
			params.put("examName", currentExamEntity.getExamname());
			params.put("score", scoreService.formatScoreStr(scoreEntity));
			params.put("remark","记得再接再厉哦");
			org.jeecgframework.core.util.LogUtil.info(params);
			noticeService.sendStudentScoreNoticeToUser(openId, params);
		}
		
		j.setSuccess(true);
		return j;
	}	
	
	
	
	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(NoticeEntity notice,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(NoticeEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, notice, request.getParameterMap());
		this.noticeService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除用于存放绑定用户的信息
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(NoticeEntity notice, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		notice = systemService.getEntity(NoticeEntity.class, notice.getId());
		message = "用于存放绑定用户的信息删除成功";
		noticeService.delete(notice);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加用于存放绑定用户的信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(NoticeEntity notice, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(notice.getId())) {
			message = "用于存放绑定用户的信息更新成功";
			NoticeEntity t = noticeService.get(NoticeEntity.class, notice.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(notice, t);
				noticeService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "用于存放绑定用户的信息更新失败";
			}
		} else {
			message = "用于存放绑定用户的信息添加成功";
			noticeService.save(notice);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 用于存放绑定用户的信息列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(NoticeEntity notice, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(notice.getId())) {
			notice = noticeService.getEntity(NoticeEntity.class, notice.getId());
			req.setAttribute("noticePage", notice);
		}
		return new ModelAndView("com/canxin/notice/notice");
	}
}
